<?php
namespace App\Job;

use App\Helper\TimeHelper;
use Hyperf\AsyncQueue\Job;
use Hyperf\DbConnection\Db;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Utils\ApplicationContext;
use App\Service\RugbyReviewService;
use App\Helper\SeoHelper;

class RugbyReviewJob extends Job
{
    protected $product_id;
    protected $language_id;
    protected $productname;
    protected $productdesc;
    protected $language;
    protected $webname;

    protected $logger;

    public function __construct(int $product_id, int $language_id, string $productname, string $productdesc, string $language, string $webname)
    {
        $this->product_id = $product_id;
        $this->language_id = $language_id;
        $this->productname = $productname;
        $this->productdesc = $productdesc;
        $this->language = $language;
        $this->webname = $webname;
    }

    public function handle()
    {
        try {
            $this->logger = ApplicationContext::getContainer()->get(LoggerFactory::class)->get('article', 'article');

            $reviewService = ApplicationContext::getContainer()->get(RugbyReviewService::class);

            $revie = $reviewService->generateReview([
                'product_id' => $this->product_id,
                'product_name' => $this->productname,
                'product_description' => $this->productdesc,
                'language' => $this->language,
                'language_id' => $this->language_id,
            ]);

            if (empty($revie)) {
                $this->logger->warning("AI返回评论为空", [
                    'product_name' => $this->productname,
                    'webname' => $this->webname
                ]);
                return;
            }

            $this->logger->info("AI返回评论", $revie);

            $conn = Db::connection($this->webname);
            $insert_data = [];
            foreach ($revie as $val){
                $rating = rand(3,5);
                $avatar_num = str_pad(rand(1, 83), 2, '0', STR_PAD_LEFT);

                $insert_data[] = [
                    'product_id'    => $this->product_id,
                    'customer_id'   => 0,
                    'author'        => $val['author'],
                    'text'          => $val['text'],
                    'rating'        => $rating,
                    'status'        => 1,
                    'avatar'        => 'catalog/avatar/avatar_'.$avatar_num.'.jpg',
                    'date_added'    => TimeHelper::randomRecentDatetime(3),
                    'date_modified' => TimeHelper::randomRecentDatetime(3),
                ];
            }

            if(!empty($insert_data)){
                $conn->table('oc_review')->insert($insert_data);
            }

            $this->logger->info("评论生成成功", [
                'product_name' => $this->productname,
                'webname' => $this->webname
            ]);
        } catch (\Throwable $e) {
            $this->logger->error("评论任务处理失败", [
                'product_name' => $this->productname,
                'error' => $e->getMessage(),
                'trace' => $e->getTraceAsString()
            ]);
            throw $e;
        }
    }
}
